<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>力扣-115-不同的子序列 | 园田家</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="力扣：115.不同的子序列今天的力扣每日一题是《不同的子序列》，这是一道困难题。虽然是困难题，并且一看题目就感觉又又又不会做了，，但是自己还是试着想一想怎么做。毕竟拿到题不思考一下就去看题解，就不叫做题了。首先我马上想到了滑动窗口，然后看到了给的示例，这不对啊，子序列不是连着的也行。接着我看了下这道题的标签，上面写着“动态规划”。啊这，动态规划就是包含现在的数或者不包含现在的数。最后，不行了，还是">
<meta property="og:type" content="article">
<meta property="og:title" content="力扣-115-不同的子序列">
<meta property="og:url" content="http://example.com/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/index.html">
<meta property="og:site_name" content="园田家">
<meta property="og:description" content="力扣：115.不同的子序列今天的力扣每日一题是《不同的子序列》，这是一道困难题。虽然是困难题，并且一看题目就感觉又又又不会做了，，但是自己还是试着想一想怎么做。毕竟拿到题不思考一下就去看题解，就不叫做题了。首先我马上想到了滑动窗口，然后看到了给的示例，这不对啊，子序列不是连着的也行。接着我看了下这道题的标签，上面写着“动态规划”。啊这，动态规划就是包含现在的数或者不包含现在的数。最后，不行了，还是">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/images/leetcode-115/img1.png">
<meta property="article:published_time" content="2021-03-17T05:07:29.000Z">
<meta property="article:modified_time" content="2021-03-18T16:28:18.435Z">
<meta property="article:author" content="kurodasense">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/images/leetcode-115/img1.png">
  
    <link rel="alternate" href="/atom.xml" title="园田家" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">园田家</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-力扣-115-不同的子序列" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/" class="article-date">
  <time datetime="2021-03-17T05:07:29.000Z" itemprop="datePublished">2021-03-17</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      力扣-115-不同的子序列
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="力扣：115-不同的子序列"><a href="#力扣：115-不同的子序列" class="headerlink" title="力扣：115.不同的子序列"></a>力扣：115.不同的子序列</h3><p>今天的力扣每日一题是《不同的子序列》，这是一道困难题。虽然是困难题，并且一看题目就感觉又又又<br>不会做了，，但是自己还是试着想一想怎么做。毕竟拿到题不思考一下就去看题解，就不叫做题了。<br>首先我马上想到了滑动窗口，然后看到了给的示例，这不对啊，子序列不是连着的也行。接着我看了下这道题的标签，<br>上面写着“动态规划”。啊这，动态规划就是包含现在的数或者不包含现在的数。最后，不行了，还是看题解吧。<br>所幸，有一个人的题解我觉得写得很好，并且浏览数和获赞数还比官方题解高，，<br>下面，我就尝试着用自己理解的意思把这道题解释一下。</p>
<hr>
<h3 id="题目描述："><a href="#题目描述：" class="headerlink" title="题目描述："></a>题目描述：</h3><p>给定一个字符串 <code>s</code> 和一个字符串 <code>t</code> ，计算在 <code>s</code> 的子序列中 <code>t</code> 出现的个数。<br>字符串的一个 子序列 是指，通过删除一些（也可以不删除）字符且不干扰剩余字符相<br>对位置所组成的新字符串。（例如，<code>ACE</code>是<code>ABCDE</code>的一个子序列，而<code>AEC</code>不是）<br>题目数据保证答案符合 <code>32</code> 位带符号整数范围。</p>
<p>示例 1：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">输入：s &#x3D; &quot;rabbbit&quot;, t &#x3D; &quot;rabbit&quot;</span><br><span class="line">输出：3</span><br><span class="line">解释：</span><br><span class="line">如下图所示, 有 3 种可以从 s 中得到 &quot;rabbit&quot; 的方案。</span><br><span class="line">(上箭头符号 ^ 表示选取的字母)</span><br><span class="line">rabbbit</span><br><span class="line">^^^^ ^^</span><br><span class="line">rabbbit</span><br><span class="line">^^ ^^^^</span><br><span class="line">rabbbit</span><br><span class="line">^^^ ^^^</span><br></pre></td></tr></table></figure>

<p>示例 2：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">输入：s &#x3D; &quot;babgbag&quot;, t &#x3D; &quot;bag&quot;</span><br><span class="line">输出：5</span><br><span class="line">解释：</span><br><span class="line">如下图所示, 有 5 种可以从 s 中得到 &quot;bag&quot; 的方案。 </span><br><span class="line">(上箭头符号 ^ 表示选取的字母)</span><br><span class="line">babgbag</span><br><span class="line">^^ ^</span><br><span class="line">babgbag</span><br><span class="line">^^    ^</span><br><span class="line">babgbag</span><br><span class="line">^    ^^</span><br><span class="line">babgbag</span><br><span class="line">  ^  ^^</span><br><span class="line">babgbag</span><br><span class="line">    ^^^</span><br></pre></td></tr></table></figure>

<p>我的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">numDistinct</span><span class="params">(String s, String t)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">char</span>[] sc = s.toCharArray();</span><br><span class="line">        <span class="keyword">char</span>[] tc = t.toCharArray();</span><br><span class="line">        <span class="keyword">int</span> n_tc = tc.length;</span><br><span class="line">        <span class="keyword">int</span> n_sc = sc.length;</span><br><span class="line">        <span class="keyword">int</span>[][] dp = <span class="keyword">new</span> <span class="keyword">int</span>[n_tc + <span class="number">1</span>][n_sc + <span class="number">1</span>];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;n_sc+<span class="number">1</span>;i++)&#123;</span><br><span class="line">            dp[<span class="number">0</span>][i] = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;n_tc+<span class="number">1</span>;i++)&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;n_sc+<span class="number">1</span>;j++)&#123;</span><br><span class="line">                <span class="keyword">if</span>(tc[i-<span class="number">1</span>] == sc[j-<span class="number">1</span>])&#123;</span><br><span class="line">                    dp[i][j] = dp[i-<span class="number">1</span>][j-<span class="number">1</span>] + dp[i][j-<span class="number">1</span>];</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    dp[i][j] = dp[i][j-<span class="number">1</span>];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> dp[n_tc][n_sc];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这道题得用动态规划来做。<br>我们创建一个<code>dp[t.length() + 1][s.length() + 1]</code>数组来计算我们的子序列个数。为什么要<code>+1</code>呢？<br>因为我们包含了空字符串，空字符串也是一个字符串的子序列，这就像空集是任何一个集合的子集一样。<br>然后，这个数组中的元素<code>dp[i][j]</code>表示：在<code>s</code>的前<code>j</code>个字符中，包含<code>t</code>的前<code>i</code>个字符的个数。<br>接着，我们就能得出一个这样的方程：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">当t[i] &#x3D;&#x3D; s[j]时，dp[i][j] &#x3D; dp[i-1][j-1] + dp[i][j-1]</span><br><span class="line">当t[i] !&#x3D; s[j]时，dp[i][j] &#x3D; dp[i][j-1]</span><br></pre></td></tr></table></figure>
<p>拿<code>s = &quot;babgbag&quot;, t = &quot;bag&quot;</code>来举例，我们可以得出下面的矩阵：<br><img src="/images/leetcode-115/img1.png" alt="leetcode上@powcai的图">  </p>
<p><code>dp[i][j-1]</code>是在<code>s</code>的前<code>j-1</code>的字符中包含<code>t</code>的前<code>i</code>的字符的个数，而<code>dp[i-1][j-1]</code>是<code>s</code>的前<code>j-1</code>的字符中包含<code>t</code>的<br>前<code>i-1</code>的字符的个数，也就是说<code>dp[i-1][j-1]</code>是不包含的<code>t[i]</code>。当<code>t[i] == s[j]</code>时，前面<code>i-1</code>的字符就可以和第<code>i</code>个字符串组合起来，<br>然后再加上<code>[i][j-1]</code>，这个在<code>j-1</code>里面已经能找到i前面字符的个数，就是<code>dp[i][j]</code>的个数。</p>
<hr>
<p>我写起来可能写得不够清楚，但是填一下上面的矩阵，然后再稍微想一下，思路大概就通了。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/" data-id="ckmd1miab0000mov5e548clyi" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          力扣-92-反转链表II
        
      </div>
    </a>
  
  
    <a href="/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">力扣: 54.螺旋矩阵</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">十一月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/04/18/leetcode-26/">leetcode-26</a>
          </li>
        
          <li>
            <a href="/2021/03/21/hasNext-%E4%B8%8EhasNextLine/">hasNext()与hasNextLine()</a>
          </li>
        
          <li>
            <a href="/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/">力扣-92-反转链表II</a>
          </li>
        
          <li>
            <a href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/">力扣-115-不同的子序列</a>
          </li>
        
          <li>
            <a href="/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/">力扣: 54.螺旋矩阵</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 kurodasense<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>